function p_move(player, dir, map, c_type)
	pX = player.x/tile_size
	pY = player.y/tile_size
	--print(table.tostring(map))
	local walk_stam = 1
	local diag_stam = 30

	TEsound.stop("player_step")



	if c_type == "tiled" then


			if dir == "up" then
				--if map[pY-1][px] ~= nil then
					if map[pY-1][pX].type == "wall" then
						print("cannot move this way")
						printc("cannot move this way")
					elseif p_use_stam(walk_stam) then
						player.y = player.y - tile_size
						p_walk_sound()
					end
				--end
			elseif dir == "down" then
				--if map[pY+1][px] ~= nil then
					if map[pY+1][pX].type == "wall" then
						print("cannot move this way")
						printc("cannot move this way")
					elseif p_use_stam(walk_stam) then
						player.y = player.y + tile_size
						p_walk_sound()

					end
				--end
			elseif dir == "right" then
				--if map[pY][pX+1] ~= nil then
					if map[pY][pX+1].type == "wall" then
						print("cannot move this way")
						printc("cannot move this way")
					elseif p_use_stam(walk_stam) then
						player.x = player.x + tile_size
						p_walk_sound()
					end
				--end
			elseif dir == "left" then
				--if map[pY][pX-1] ~= nil then
					if map[pY][pX-1].type == "wall" then
						print("cannot move this way")
						printc("cannot move this way")
					else
						player.x = player.x - tile_size
						p_use_stam(walk_stam)
						p_walk_sound()
					end
				--end
			elseif dir == "up-left" then
				if map[pY-1][pX-1].type == "wall" then
					print("cannot move this way")
					printc("cannot move this way")
				elseif p_use_stam(diag_stam) then
					player.x = player.x - tile_size
					player.y = player.y - tile_size

				end
			elseif dir == "down-left" then
				if map[pY+1][pX-1].type == "wall" then
					print("cannot move this way")
					printc("cannot move this way")
				elseif p_use_stam(diag_stam) then
					player.x = player.x - tile_size
					player.y = player.y + tile_size

				end
			elseif dir == "up-right" then
				if map[pY-1][pX+1].type == "wall" then
					print("cannot move this way")
					printc("cannot move this way")
				elseif p_use_stam(diag_stam) then
					player.x = player.x + tile_size
					player.y = player.y - tile_size

				end
			elseif dir == "down-right" then
				if map[pY+1][pX+1].type == "wall" then
					print("cannot move this way")
					printc("cannot move this way")
				elseif p_use_stam(diag_stam) then
					player.x = player.x + tile_size
					player.y = player.y + tile_size

				end
			end

	else
		if dir == "up" then
			--if map[pY-1][px] ~= nil then
			if map[pY-1][pX] == wall_tile then
				print("cannot move this way")
				printc("cannot move this way")

			elseif p_use_stam(walk_stam) then
				player.y = player.y - tile_size
				p_walk_sound()
			end
			--end
		elseif dir == "down" then
			--if map[pY+1][px] ~= nil then
			if map[pY+1][pX] == wall_tile then
				print("cannot move this way")
				printc("cannot move this way")

			elseif p_use_stam(walk_stam) then
				player.y = player.y + tile_size
				p_walk_sound()
			end
			--end
		elseif dir == "right" then
			--if map[pY][pX+1] ~= nil then
			if map[pY][pX+1] == wall_tile then
				print("cannot move this way")
				printc("cannot move this way")

			elseif p_use_stam(walk_stam) then
				player.x = player.x + tile_size
				p_walk_sound()
			end
			--end
		elseif dir == "left" then
			--if map[pY][pX-1] ~= nil then
			if map[pY][pX-1] == wall_tile then
				print("cannot move this way")
				printc("cannot move this way")

			elseif p_use_stam(walk_stam) then
				player.x = player.x - tile_size
				p_walk_sound()
			end
			--end
		elseif dir == "up-left" then
			if map[pY-1][pX-1] == wall_tile then
				print("cannot move this way")
				printc("cannot move this way")
			elseif p_use_stam(diag_stam) then
				player.x = player.x - tile_size
				player.y = player.y - tile_size
			end
		elseif dir == "down-left" then
			if map[pY+1][pX-1] == wall_tile then
				print("cannot move this way")
				printc("cannot move this way")
			elseif p_use_stam(diag_stam) then
				player.x = player.x - tile_size
				player.y = player.y + tile_size
			end
		elseif dir == "up-right" then
			if map[pY-1][pX+1] == wall_tile then
				print("cannot move this way")
				printc("cannot move this way")
			elseif p_use_stam(diag_stam) then
				player.x = player.x + tile_size
				player.y = player.y - tile_size
			end
		elseif dir == "down-right" then
			if map[pY+1][pX+1] == wall_tile then
				print("cannot move this way")
				printc("cannot move this way")
			elseif p_use_stam(diag_stam) then
				player.x = player.x + tile_size
				player.y = player.y + tile_size
			end
		end
	end


end

function p_range (x, y)

	local distX = data.player.x - x
	local distY = data.player.y - y

	local absDistX = math.abs(distX)
	local absDistY = math.abs(distY)

	return absDistX, absDistY


end

function p_range_t(x,y)
	local distX = data.player.x - x
	local distY = data.player.y - y

	return distX, distY

end

function p_finished_turn()
	p_gain_stam(data.player.stam_regen)
	p_heal(data.player.health_regen, 0)
	p_auto_pickup()
	p_on_meta()
	enemy_hud()
	if local_map.items ~= nil then
		move_projectiles()
	end

	if local_map.features ~= nil then
		p_impact_features(local_map.features)
	end
	ai_projectiles()
	p_damage_enemies(local_map.enemies)

	e_cleanup(local_map.enemies)
	p_level_check()
	data.player.turn = false

end

function p_on_meta()

	if local_map.meta_tiles ~= nil then

		for i = 1, #local_map.meta_tiles do
			--print(table.tostring(local_map.meta_tiles[i]))
			if p_loc(local_map.meta_tiles[i].x,
				local_map.meta_tiles[i].y) then
				if local_map.meta_tiles[i].name ~= nil then
					print("on meta tile", local_map.meta_tiles[i].name)
					local ns = append_strings("standing on ", local_map.meta_tiles[i].name)
					printc(ns)
				end

			end

		end

	end

end

function p_loc(x,y)

	if data.player.x == x and data.player.y == y then

		return true
	else

		return false

	end

end



function p_impact_features(features)
	for i = 1, #features do

		if features[i].tt == "computer" then
			--print("computer hit")
			if features[i].x == data.player.x
				and features[i].y == data.player.y then

				features[i].image = computer_off_img

				for j = #features, 1, -1 do

					if features[j].tt == "nerd" then

						if features[j].id == features[i].id then

							table.remove(features, j)

						end
					end

				end

			end

		end

	end

end

function p_death()

	if data.player.health <= 0 then

		gv:gotoState('Defeat')

	end

end

function p_update()

	p_death()

end

function p_use_stam(amount)

	if data.player.stamina >= amount then
		data.player.stamina = data.player.stamina - amount
		return true
	else
		printc("not enough stamina")
		return false
	end

end

function p_gain_stam(amount)

	if data.player.stamina+amount <= data.player.max_stam then
		data.player.stamina = data.player.stamina + amount
	else
		data.player.stamina = data.player.max_stam
	end

end

function p_damage_enemies(enemies)
	if enemies ~= nil then
		for i = 1, #enemies do
			if data.player.x == enemies[i].x and data.player.y == enemies[i].y then
				enemies[i].health = enemies[i].health - data.player.damage
			end
		end
	end
end

function p_damage_enemy(enemy, damage)
	enemy.health = enemy.health - damage

end

function p_level_check()


	local base_lev = 100
	local increase_mod = 10



	data.player.req_xp = base_lev + (increase_mod*data.player.level)

	if data.player.xp >= data.player.req_xp then

		local rh = RNG:random(5)
		local rs = RNG:random(10)
		data.player.level = data.player.level+1
		data.player.max_health = data.player.max_health+rh
		data.player.max_stam = data.player.max_stam+rs
		data.player.xp = data.player.xp-data.player.req_xp
	end

end

function p_use_hands(key, unicode, dir, map, player, c_type)
	--print(table.tostring(map))
	--print("using left_hand")

	local ns = local_map.name:sub(1,7)

	if ns == "harvest" then
		if data.player.left_hand[1] ~= nil then
			print(data.player.left_hand[1].name)
			if data.player.left_hand[1].name == "hatchet" then
				if data.player.left_hand[1].uses > 0 then
					hatchet_chop(dir, map, player, c_type)
					data.player.left_hand[1].uses = data.player.left_hand[1].uses-1
					p_item_cleanup()
				end
			else
			end

		else

		end

	end
end

function p_item_cleanup()

	for i = #data.player.inventory, 1, -1 do
		if data.player.inventory[i].uses ~= nil then
			if data.player.inventory[i].uses == 0 then
				table.remove(data.player.inventory, i)
			end
		end
	end

	for i = #data.player.right_hand, 1, -1 do
		if data.player.right_hand[i].uses ~= nil then
			if data.player.right_hand[i].uses == 0 then
				table.remove(data.player.right_hand, i)
			end
		end
	end

	for i = #data.player.left_hand, 1, -1 do
		if data.player.left_hand[i].uses ~= nil then
			if data.player.left_hand[i].uses == 0 then
				table.remove(data.player.left_hand, i)
			end
		end
	end

end

function p_turn()
	if data.player.turn == true then
		data.player.turn = false
	elseif data.player.turn == false then
		data.player.turn = true
	end

end

function p_fov(x, y)

	distX, distY = p_range(x,y)

	if distX == player_fov*tile_size
		and distY == player_fov*tile_size then
		return false
	elseif distX <= player_fov*tile_size
		and distY <= player_fov*tile_size then
		return true
	else
		return false
	end


end

function p_heal(amount, cost)

	if data.player.health+amount <= data.player.max_health then

		data.player.health = data.player.health + amount
	else

		data.player.health = data.player.max_health

	end

	data.player.seeds = math.floor(data.player.seeds-cost)

end

function p_seed_heal()

	local healCost = math.floor(data.player.heal_amount * data.player.heal_cost)

	if data.player.seeds >= healCost then

		p_heal(data.player.heal_amount, healCost)
	else
		printc("Not enough seeds!")
	end

	p_finished_turn()

end

function p_add_seeds(amount)

	data.player.seeds = data.player.seeds + amount

end

function p_add_bullets(amount)

	data.player.bullets = data.player.bullets+amount

end

function p_auto_pickup()
	for i = #local_map.items, 1, -1 do

		if local_map.items[i].it == "seedbag" then
			if local_map.items[i].x == player.x
				and local_map.items[i].y == player.y then
				p_add_seeds(local_map.items[i].amount)
				table.remove(local_map.items, i)
			end
		elseif local_map.items[i].it == "bullets" then
			if local_map.items[i].x == player.x
				and local_map.items[i].y == player.y then
				p_add_bullets(local_map.items[i].amount)
				TEsound.play(gun_cocking3_aud, "clip-pull-cocking")
				table.remove(local_map.items, i)
			end

		end

	end

end

function p_quest_turn_in(npc)
	--print("in p_quest_turn_in")
	if data.player.inventory ~= nil then
		--print("in if")


		if #data.player.inventory == 0 then
			local qs = "You do not have a "
			local ss = append_strings(qs, npc.vq)
			printc(ss)

		else

			for i = #data.player.inventory, 1, -1 do
				print("name : ", data.player.inventory[i].name)
				if data.player.inventory[i].name == npc.vq then

					data.player.seeds = data.player.seeds + data.player.inventory[i].value*8
					table.remove(data.player.inventory, i)
					npc.q_given = false
					print("victory condition met")
					gv:gotoState('Victory')
				else
					local qs = "You do not have a "
					local ss = append_strings(qs, npc.vq)
					printc(ss)

				end

			end

		end
	else
		local qs = "You do not have a "
		local ss = append_strings(qs, npc.vq)
		printc(ss)
	end

end

function p_accept_quest(npc)



	if #data.player.quests < 2 then
		print("quest for ", npc.vq)
		local qs = append_strings("quest for ", npc.vq)
		printc(qs)
		table.insert(data.player.quests, npc.vq)
		npc.q_given = true

	else

		printc("quest log full, finish quests!")
		npc.q_given = false

	end

end

function p_add_inv(item)

	table.insert(data.player.inventory, item)

end

function p_rem_inv(item)



end

function p_target()
	data.player.targeting = true
	t_cursor.x = data.player.x
	t_cursor.y = data.player.y

end

function basic_controls(key,unicode, cur_map, c_type) -- REMEMBER LOCAL CUR_MAP

	--print(table.tostring(cur_map))

	if data.player.targeting == true then
		shooting_controls(key,unicode, cur_map)
	elseif data.player.drop_mode == true then
		if key == "`" then
			data.player.text_input = true
		end
		if data.player.text_input == true then
			p_input_text(key, unicode)
		else
			if unicode >= 49 and unicode <= 57 then
				select_string = string.char(unicode)
				select_int = 0 + select_string

				p_drop_item(select_int)
			else
				data.player.drop_mode = false
			end
		end
	elseif data.player.equip_mode == "right" then
		if key == "`" then
			data.player.text_input = true
		end
		if data.player.text_input == true then
			p_input_text(key, unicode)
		else
			if unicode >= 49 and unicode <= 57 then
				select_string = string.char(unicode)
				select_int = 0 + select_string

				p_equip_item("right", select_int)
			elseif key == "u" then
				p_unequip_item("right")
			else
				data.player.equip_mode = "none"
			end
		end
	elseif data.player.equip_mode == "left" then
		if key == "`" then
			data.player.text_input = true
		end
		if data.player.text_input == true then
			p_input_text(key, unicode)
		else
			if unicode >= 49 and unicode <= 57 then
				select_string = string.char(unicode)
				select_int = 0 + select_string

				p_equip_item("left", select_int)
			elseif key == "u" then
				p_unequip_item("left")
			else
				data.player.equip_mode = "none"

			end
		end

	elseif data.player.text_input == true then
		p_input_text(key, unicode)

	else
		if key == "up" then

			p_use_hands(key, unicode, key, cur_map, data.player, c_type)
			p_move(data.player, key , cur_map, c_type)
			p_finished_turn()


		elseif key == "down" then
			p_use_hands(key, unicode, key, cur_map, data.player, c_type)
			p_move(data.player, key , cur_map, c_type)
			p_finished_turn()


		elseif key == "right" then
			p_use_hands(key, unicode, key, cur_map, data.player, c_type)
			p_move(data.player, key , cur_map, c_type)
			p_finished_turn()


		elseif key == "left" then
			p_use_hands(key, unicode, key, cur_map, data.player, c_type)
			p_move(data.player, key , cur_map, c_type)
			p_finished_turn()


		elseif key == "f" then
			data.player.targeting = true
			t_cursor.x = data.player.x
			t_cursor.y = data.player.y
		elseif key == "`" then
			data.player.text_input = true
		elseif key == "r" then

			data.player.equip_mode = "right"

		elseif key == "l" then
			data.player.equip_mode = "left"
		elseif key == "d" then
			if data.player.drop_mode == false then
				data.player.drop_mode = true
			else
				data.player.drop_mode = false
			end
		elseif key == "g" then
			for i = 1, #data.player.local_map.items do
				if data.player.local_map.items[i] ~= nil then
					p_pickup_item(i)

				end
			end
		elseif key == "h" then
			p_seed_heal()
		elseif key == "kp*" then
			if data.fps == true then
				data.fps = false
			elseif data.fps == false then
				data.fps = true
			end
		elseif key == " " then
			p_finished_turn()
		elseif key == "w" then
		elseif key == "s" then
		elseif key == "d" then
		elseif key == "l" then

		end

		if love.keyboard.isDown("up") and love.keyboard.isDown("left") then
			p_move(data.player, "up-left", cur_map, c_type)


		end

		if love.keyboard.isDown("up") and love.keyboard.isDown("right") then
			p_move(data.player, "up-right", cur_map, c_type)

		end

		if love.keyboard.isDown("down") and love.keyboard.isDown("left") then
			p_move(data.player, "down-left", cur_map, c_type)

		end

		if love.keyboard.isDown("down") and love.keyboard.isDown("right") then
			p_move(data.player, "down-right", cur_map, c_type)

		end

	end

end


function shooting_controls(key, unicode, cur_map)

	if key == 'f' then
		p_shoot(t_cursor)
		data.player.targeting = false
	elseif key == 'return' then
		p_shoot(t_cursor)
		data.player.targeting = false
	elseif key == 'escape' then
		data.player.targeting = false
	elseif key == 'up' then
		p_move(t_cursor, "up", cur_map)
	elseif key == 'down' then
		p_move(t_cursor, "down", cur_map)
	elseif key == 'right' then
		p_move(t_cursor, "right", cur_map)
	elseif key == 'left' then
		p_move(t_cursor, "left", cur_map)
	end

end

function draw_player()
	local p_scale_x = scale_size/2*tile_size
	local p_scale_y = scale_size/2*tile_size
	player = data.player


	love.graphics.draw(player.image,
		player.x, player.y)

	if player.targeting == true then
		love.graphics.draw(t_cursor.image,
			t_cursor.x, t_cursor.y)
	end


	--love.graphics.draw(player.image,
	--	p_scale_x, p_scale_y)

	if data.player.right_hand[1] ~= nil then

		if data.player.right_hand[1].it == "gun" then

			love.graphics.draw(data.player.right_hand[1].image,
				player.x, player.y+(tile_size/4), 0, -0.5, 0.55, tile_size, 0)
		elseif data.player.right_hand[1].it == "grenade" then

			love.graphics.draw(data.player.right_hand[1].image,
				player.x, player.y+(tile_size/4), 0, -0.5, 0.5, tile_size, 0)

		end

	end

	if data.player.left_hand[1] ~= nil then

		if data.player.left_hand[1].it == "grenade" then

			love.graphics.draw(data.player.left_hand[1].image,
				player.x+tile_size/2, player.y+(tile_size/4), 0, -0.5, 0.5, tile_size, 0)

		elseif data.player.left_hand[1].it == "melee" then

			love.graphics.draw(data.player.left_hand[1].image,
				player.x+tile_size/2, player.y+(tile_size/4), 0, -0.5, 0.5, tile_size, 0)

		end


	end



end

function p_shoot(target)
	if data.player.right_hand[1] ~= nil then
		if data.player.right_hand[1].it == "gun" then
			shoot_gun(target.x, target.y, data.player.right_hand[1])
		elseif data.player.right_hand[1].it == "grenade" then
			throw_grenade(target.x, target.y,
				data.player.right_hand[1], "right")

		end
	end

	if data.player.left_hand[1] ~= nil then
		if data.player.left_hand[1].it == "grenade" then
			throw_grenade(target.x, target.y,
				data.player.left_hand[1], "left")

		end
	end
end

function p_input_text(key, unicode)

	if key == 'backspace' then
		data.player.con_text = string.sub(data.player.con_text,1,-2)
	elseif unicode > 31 and unicode < 127 then

		data.player.con_text = data.player.con_text .. string.char(unicode)
	elseif key == 'escape' or key == '`' then
		data.player.text_input = false
	elseif key == 'return' then
		parse_con(data.player.con_text)
		data.player.text_input = false
	end

end

function p_equip_item(hand, itemid)

	if data.player.inventory[itemid] ~= nil then
		item_hand = data.player.inventory[itemid].hand
	end
	--[[ sets r and l hands to single inventory
	if hand == "right" and data.player.right_hand[1] ~= nil then
		table.insert(data.player.inventory, data.player.right_hand[1])
		table.remove(data.player.right_hand, 1)
	elseif hand == "left" and data.player.left_hand[1] ~= nil then
		table.insert(data.player.inventory, data.player.left_hand[1])
		table.remove(data.player.left_hand, 1)
	end
	]]


	if hand == "right" and item_hand =="right" then

		table.insert(data.player.right_hand, data.player.inventory[itemid])
		table.remove(data.player.inventory, itemid)
	elseif hand == "left" and item_hand=="left" then
		table.insert(data.player.left_hand, data.player.inventory[itemid])
		table.remove(data.player.inventory, itemid)
	elseif item_hand == "none" then
		if hand == "right" then
			table.insert(data.player.right_hand, data.player.inventory[itemid])
			table.remove(data.player.inventory, itemid)
		elseif hand == "left" then
			table.insert(data.player.left_hand, data.player.inventory[itemid])
			table.remove(data.player.inventory, itemid)
		end
	else
		printc("wrong hand")
	end

	data.player.equip_mode = "none"

end

function p_unequip_item(hand)

	if hand == "right" and data.player.right_hand[1] ~= nil then
		table.insert(data.player.inventory, data.player.right_hand[1])
		table.remove(data.player.right_hand, 1)
	elseif hand == "left" and data.player.left_hand[1] ~= nil then
		table.insert(data.player.inventory, data.player.left_hand[1])
		table.remove(data.player.left_hand, 1)
	end

end


function p_drop_item(itemid)

	if data.player.inventory[itemid] ~= nil then
		data.player.inventory[itemid].x = player.x
		data.player.inventory[itemid].y = player.y
		table.insert(data.player.local_map.items,
			data.player.inventory[itemid])
		drop_msg = append_strings(data.player.inventory[itemid].name,
			" dropped")
		printc(drop_msg)

		table.remove(data.player.inventory,itemid)
		--print(table.tostring(data.player.local_map.items))
		data.player.drop_mode = false

	end

end

function p_pickup_item(itemid)
	i = itemid
	if data.player.local_map.items[i].x == data.player.x
		and data.player.local_map.items[i].y == data.player.y then
		table.insert(data.player.inventory,
			data.player.local_map.items[i])
		local it_str = append_strings("picked up ",
			data.player.local_map.items[i].name)
		printc(it_str)
		table.remove(data.player.local_map.items, i)

	end
end

function p_goto_town()
	--p_save(data.player, maps)
	data.player.last_harvest = data.player.current_harvest
	maps.town.l_times = maps.town.l_times+1
	data.player.current_harvest = data.player.current_harvest-1
	data.player.x = maps.town.features.stair_dh.x
	data.player.y = maps.town.features.stair_dh.y
	gv:gotoState('Town')

end

function p_walk_sound()
	--print("in walk sound")
	TEsound.play(p_step_aud, "player_step")
	TEsound.volume("player_step", main_volume*0.25)

end

function p_save(player, maps)

	if data.player.current_harvest == 1 then
		local ns = data.player.current_harvest .. ""
		local nss = append_strings(data.player.local_map.name, ns)
		local nsss = append_strings(nss, ".gv")
		data.player.last_harvest = 1

		love.filesystem.write(nsss, table.show(data.player.local_map, "l_local_map"))
		data.player.local_map = nil

	end

	love.filesystem.write("player.gv", table.show(player, "loaded_player"))

end

function p_load_harvest(mode)

	if mode == nil then
		data.player.x = harvest_maps[data.player.current_harvest].features[1].x
		data.player.y = harvest_maps[data.player.current_harvest].features[1].y

		--[[
		if data.player.last_harvest == 1 then

			chunk = love.filesystem.load("harvest1.gv")

			local result = chunk()
			maps[2] = result

		end
		]]
		data.player.local_map = harvest_maps[data.player.current_harvest]
		local_map = data.player.local_map
	elseif mode == "hup" then
		print("loading")
		data.player.x = harvest_maps[data.player.current_harvest].features[2].x
		data.player.y = harvest_maps[data.player.current_harvest].features[2].y

		data.player.local_map = harvest_maps[data.player.current_harvest]
		local_map = data.player.local_map
		data.player.hup = true
	end


end

function p_random_spawn(player, grid)

	local placed = false
	local potentialX = 0
	local potentialY = 0

	while placed == false do
		potentialX = RNG:random(grid_size)
		potentialY = RNG:random(grid_size)

		if grid[potentialY][potentialX].type == "floor" then
			player.x = potentialX*tile_size
			player.y = potentialY*tile_size
			placed = true
		end
	end

end


function p_goto_map()

	if data.player.local_map.name == "town" then
		gv:gotoState('Harvest')
	end

end

function p_tidy()

	if data.player.buy_mode == false then

		for i = 1, #data.player.local_map.npcs do
			data.player.local_map.npcs[i].shopping = false

		end


	end

end
